package com.platform.modules.sys.controller;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.platform.common.utils.RestResponse;
import com.platform.common.utils.StringUtils;
import com.platform.config.DocConstants;
import com.platform.dao.AttachmentDao;
import com.platform.entity.Attachment;
import com.platform.entity.Document;
import com.platform.util.DocUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;


@Api(tags = "公共文档操作方法")
@RestController
@RequestMapping("sys/doc")
public class SysDocController extends AbstractController {
	
    @Autowired
    private DocUtil docUtil;
    
    @Autowired
    AttachmentDao attachmentDao;


    @ApiOperation(value = "上传文件")
    @PostMapping("/upload")
    public RestResponse upload(MultipartFile file, String businessId, String code) {
        docUtil.createAtt(file, code, businessId);
        return RestResponse.success();
    }


    @ApiOperation(value = "获取文件list")
    @GetMapping("/atts")
    public RestResponse atts(MultipartFile file, String businessId, String code) {
       List<Attachment> atts = docUtil.getAttListByAttBusinessIdAndAttCode(businessId,code);
        return RestResponse.success().put("list",atts);
    }



    @ApiOperation(value = "获取文件流")
    @GetMapping("/att/{attId}")
    public void att(@PathVariable String attId,HttpServletRequest request, HttpServletResponse response) {
    	
    	InputStream is = docUtil.getFile(attId);
    	
    	 // 实现文件下载
        byte[] buffer = new byte[1024];
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(is);
            OutputStream os = response.getOutputStream();
            int i = bis.read(buffer);
            while (i != -1) {
                os.write(buffer, 0, i);
                i = bis.read(buffer);
            }
        }
        catch (Exception e) {
            
        }
        finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                	is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    
    
    @ApiOperation(value = "缩略图")
    @GetMapping("/thumbnail/{attId}")
    public void thumbnail(@PathVariable String attId,HttpServletRequest request, HttpServletResponse response) {
    	
    	//InputStream is = docUtil.getFile(DocConstants.THUMBNAIL, attId);

        InputStream is = docUtil.getFile(DocConstants.THUMBNAIL, "default.png");
    	
    	 // 实现文件下载
        byte[] buffer = new byte[1024];
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(is);
            OutputStream os = response.getOutputStream();
            int i = bis.read(buffer);
            while (i != -1) {
                os.write(buffer, 0, i);
                i = bis.read(buffer);
            }
        }
        catch (Exception e) {
            
        }
        finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                	is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    
    @ApiOperation(value = "下载文件")
    @GetMapping("/att/download/{attId}")
    public void downloadAtt(@PathVariable String attId,HttpServletRequest request, HttpServletResponse response) {
    	InputStream is = docUtil.getFile(attId);
    	Attachment attachment = attachmentDao.selectById(attId);
    	
    	//实现文件下载
		byte[] buffer = new byte[1024];
		BufferedInputStream bis = null;
		try {
			response.setHeader("content-type", "application/octet-stream");
			response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(attachment.getName(), "UTF-8"));
			response.setContentType("application/octet-stream");

			bis = new BufferedInputStream(is);
			OutputStream os = response.getOutputStream();
			int i = bis.read(buffer);
			while (i != -1) {
				os.write(buffer, 0, i);
				i = bis.read(buffer);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}finally {
			if (bis != null) {
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
    
    
    
    
    @ApiOperation(value = "打包下载文件")
    @GetMapping("/zip")
    public void zip() {
    	
    	
    }
    
    
    @ApiOperation(value = "删除文件")
    @PostMapping("/delete")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "delete", name = "attIds", value = "文件id(支持多个,逗号分割)", dataType = "String")
    })
    public RestResponse delete(String attIds) {
    	if(StringUtils.isNotBlank(attIds)) {
    		docUtil.deleteAtts(Arrays.asList(attIds.split(",")));
    	}
        return RestResponse.success();
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
   
}
